/*
 * author      : 吴中勤 
 * date        : 2018/12/18
 * description : TODO
 */

package org.wzq.learn;

import java.util.ArrayList;
import java.util.List;

// -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+HeapDumpOnOutOfMemoryError
/*
javac src/org/wzq/learn/HeapOOM.java
cd src
java -Xms20M -Xmx20M -Xmn10M -XX:+HeapDumpOnOutOfMemoryError org/wzq/learn/HeapOOM

【output】
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid7547.hprof ...
Heap dump file created [27574253 bytes in 0.159 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:3210)
        at java.util.Arrays.copyOf(Arrays.java:3181)
        at java.util.ArrayList.grow(ArrayList.java:261)
        at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
        at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
        at java.util.ArrayList.add(ArrayList.java:458)
        at org.wzq.learn.HeapOOM.main(HeapOOM.java:24)

*/

public class HeapOOM {
    public static void main(String[] args) {
        List<Object> objectList = new ArrayList<>();
        while(true) {
            objectList.add(new Object());
        }
    }
}
